/**
 * 
 */
package info.yggdrasilroot.driverecorder;

import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

/**
 * @author Masakazu Kobayashi
 * @version 1.0
 */
public class CameraPreview implements SurfaceHolder.Callback {
	private SurfaceHolder holder;
	protected Camera camera;
	private SurfaceView view;

	/**
	 * コンストラクタ
	 * @param context
	 * @param view
	 */
	public CameraPreview(Context context, SurfaceView view){
		this.view = view;
		this.holder = view.getHolder();
		holder.addCallback(this);
		holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		if (camera == null) {
			Context context = view.getContext();
			((Activity)context).finish();
		} else {
			camera.stopPreview();
			setPictureFormat(format);
			setPreviewSize(width, height);
			camera.startPreview();
		}
	}
	
	/**
	 * SurfaceViewが作成されたときの処理
	 * @param holder
	 */
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		if(camera == null){
			try{
				camera = Camera.open();
			}catch(RuntimeException e){
				Context context = view.getContext();
				((Activity)context).finish();
				Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
			}
		}
		if(camera != null){
			camera.setPreviewCallback(new PreviewCallback() {
				
				@Override
				public void onPreviewFrame(byte[] data, Camera camera) {
					// TODO Auto-generated method stub
					
				}
			});
			camera.setOneShotPreviewCallback(new PreviewCallback() {
				
				@Override
				public void onPreviewFrame(byte[] data, Camera camera) {
					// TODO Auto-generated method stub
					
				}
			});
		}
		try{
			camera.setPreviewDisplay(holder);
		}catch(IOException e){
			camera.release();
			camera = null;
			Context context = view.getContext();
			((Activity)context).finish();
			Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
		}
	}
	
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		if (camera != null) {
			camera.stopPreview();
			camera.release();
			camera = null;
		}
	}

	
	protected void setPictureFormat(int format) {
		try {
			Camera.Parameters params = camera.getParameters();
			List<Integer> supported = params.getSupportedPictureFormats();
			if (supported != null) {
				for (int f : supported) {
					if (f == format) {
						params.setPreviewFormat(format);
						camera.setParameters(params);
						break;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void setPreviewSize(int width, int height) {
		Camera.Parameters params = camera.getParameters();
		List<Camera.Size> supported = params.getSupportedPreviewSizes();
		if (supported != null) {
			for (Camera.Size size : supported) {
				if (size.width <= width && size.height <= height) {
					params.setPreviewSize(size.width, size.height);
					camera.setParameters(params);
					break;
				}
			}
		}
	}
}
